package com.wjh.yygh.user.api;

import com.wjh.yygh.common.result.Result;
import com.wjh.yygh.model.user.Patient;
import com.wjh.yygh.user.service.PatientService;
import com.wjh.yygh.utils.AuthContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
@Api(tags = "就诊人管理")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    @ApiOperation(value = "获取就诊人")
    @GetMapping("/auth/findAll")
    private Result<List<Patient>> findAll(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patients =  patientService.findAllByUserId(userId);
       return Result.ok(patients);
    }

    @PostMapping("/auth/save")
    public Result savePatient(@RequestBody(required = true) Patient patient, HttpServletRequest request){
//        获取当前用户登录的id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    @GetMapping("/auth/get/{id}")
    public Result<Patient> getPatientById(@PathVariable("id") Long id){
        Patient byId = patientService.getPatientById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "修改就诊人信息")
    @PostMapping("/auth/get")
    public Result<Patient> updatePatientById(Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    @ApiOperation(value = "删除就诊人信息")
    @DeleteMapping("/auth/remove/{id}")
    public Result<Patient> removePatientById(@PathVariable("id") Long id){
        patientService.removeById(id);
        return Result.ok();
    }

}
